package com.feizhi.inventory.fragment.exportdetails.exportloss;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.aserbao.aserbaosandroid.functions.database.greenDao.db.DaoSession;
import com.aserbao.aserbaosandroid.functions.database.greenDao.db.TaskDetailsDao;
import com.feizhi.inventory.MainActivity;
import com.feizhi.inventory.R;
import com.feizhi.inventory.database.TaskDetails;
import com.feizhi.inventory.database.dbApp;
import com.feizhi.inventory.databinding.FragmentInventoryLossBinding;
import com.feizhi.inventory.util.ThreadPool;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;
import java.util.List;


public class ExportLossFragment extends Fragment {
    List<TaskDetails> lists;
    private FragmentInventoryLossBinding mBinding;

    private TaskDetailsDao taskDetailsDao;
    private MainActivity mActivity;
    private ExportLossAdapter adapter;
    List<TaskDetails> taskDetailsList = new ArrayList<>();
    private String task_id;
    private ProgressDialog progressDialog;

    public ExportLossFragment() {

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (MainActivity) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mBinding = FragmentInventoryLossBinding.inflate(inflater);
        return mBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        setOnClickListener();
    }

    //控件的点击事件
    private void setOnClickListener() {
        adapter.setItemOnClickListener(new ExportLossAdapter.OnClickListener() {
            @Override
            public void OnItemClickListener(int position) {

                //打包数据去下一个页面
                Bundle bundle = new Bundle();
                bundle.putString("id", taskDetailsList.get(position).getId());
                bundle.putString("name", taskDetailsList.get(position).getName());
                bundle.putString("spec", taskDetailsList.get(position).getSpec());
                bundle.putString("eps_id", taskDetailsList.get(position).getEpcID());
                bundle.putString("save_place", taskDetailsList.get(position).getSavePlace());
                bundle.putString("custodian", taskDetailsList.get(position).getCustodian());
                bundle.putString("department", taskDetailsList.get(position).getDepartment());
                bundle.putString("state", taskDetailsList.get(position).getState());
                bundle.putString("notes_1", taskDetailsList.get(position).getNotes_1());
                bundle.putString("notes_2", taskDetailsList.get(position).getNotes_2());
                bundle.putString("notes_3", taskDetailsList.get(position).getNotes_3());
                bundle.putString("type", taskDetailsList.get(position).getType());
                bundle.putString("inputDate", taskDetailsList.get(position).getInputDate());
                Navigation.findNavController(mActivity, R.id.fragment_my).navigate(R.id.itemDetailsFragment,bundle);

            }
        });

    }

    private void initView() {

        //创建GreenDao对象，初始化
        DaoSession daoSession =((dbApp) mActivity.getApplication()).getDaoSession();
        taskDetailsDao = daoSession.getTaskDetailsDao();

        //初始化，并设置RecyclerView的Adapter
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new ExportLossAdapter(taskDetailsList, mActivity);
        mBinding.recyclerView.setAdapter(adapter);

        //接收上一个页面发送过来的数据
        LiveEventBus.get().with("tid", String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {


                task_id = s;

                onRefresh();
            }
        });

    }

    public void onRefresh() {

        refresh();

    }

    private void refresh() {
        //创建一个进度条圈圈对话框
        progressDialog = new ProgressDialog(mActivity);
        progressDialog.setTitle("加载数据");
        progressDialog.setMessage("正在加载ing...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.show();
        ThreadPool.runThread(() -> {
            lists = taskDetailsDao.queryBuilder().where(TaskDetailsDao.Properties.TaskID.eq(task_id),TaskDetailsDao.Properties.State.eq("盘亏")).list();

            if (!taskDetailsList.isEmpty()) {
                taskDetailsList.clear();
            }

            taskDetailsList.addAll(lists);

            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    adapter.setData(taskDetailsList);
                    progressDialog.dismiss();
                }
            });
        });
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//
//
//
//            }
//        }).start();


    }

}